package com.fyl.pattern.singleton.demo3;

/**
 * 饿汉式
 */
public class Singleton {
    public Singleton() {
    }

    private static volatile Singleton instance;

    public static synchronized Singleton getInstance() {
        //双重检查锁
        //1.第一次判断，如果instance的值不为null，不需要抢占锁，直接返回对象
        if (instance == null) {
            //线程1、线程2可能同时会进来
            synchronized (Singleton.class) {
                //第二次判断
                if (instance == null) {
                    instance = new Singleton();
                }
            }

        }
        return instance;
    }
}
